package com.steak.system.controller.artisan;

import com.auth0.jwt.JWT;
import com.steak.system.common.annotation.Permission;
import com.steak.system.common.base.BaseController;
import com.steak.system.common.exception.BusinessException;
import com.steak.system.common.resultType.Result;
import com.steak.system.pojo.dataobject.CollegeDO;
import com.steak.system.pojo.dto.ApplyDTO;
import com.steak.system.pojo.dto.CheckDTO;
import com.steak.system.pojo.dto.InterviewResultDTO;
import com.steak.system.pojo.dto.RecruitDTO;
import com.steak.system.service.artisan.ArtisanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.jms.Topic;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/artisan")
public class ArtisanController extends BaseController {

    @Autowired
    private ArtisanService artisanService;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Topic topic2;
    @Autowired
    private Topic topic3;
    /**
     * 发布招聘计划书
     * @return
     */
    @PostMapping("/releasePlan")
    public Result releasePlan(HttpServletRequest request,@RequestParam String title , @RequestParam int num,@RequestParam String start,
                              @RequestParam String end,@RequestParam int department,@RequestParam String requirements){
        String token = request.getHeader("TOKEN");
        String userId = JWT.decode(token).getClaim("userId").asString();
        artisanService.releasePlan(title,num,start,end,department,requirements,userId);
        return Result.create("success");
    }

    /**
     * 查询所有部门
     * @return
     */
    @GetMapping("/getAllDepartment")
    public Result getAllDepartment(){
        List<CollegeDO> list = artisanService.getAllDepartment();
        return Result.create(list);
    }

    /**
     * 招聘计划书列表
     */
    @GetMapping("/getAllRecruit")
    public Result getAllRecruit(@RequestParam int page) throws BusinessException {
        List<RecruitDTO> list = artisanService.getAllRecruit(page);
        return Result.create(list);
    }

    /**
     * 根据实验技术人员ID查找他发布的招聘计划书
     * @param request
     * @return
     */
    @GetMapping("/getReleaseRecruitByUserId")
    public Result getReleaseRecruitByUserId(HttpServletRequest request){
        String token = request.getHeader("TOKEN");
        String userId = JWT.decode(token).getClaim("userId").asString();
        List<RecruitDTO> list = artisanService.getReleaseRecruitByUserId(userId);
        return Result.create(list);
    }

    /**
     * 根据ID查询计划书信息
     * @param recruitId
     * @return
     */
    @Permission(roles = {"admin","worker","artisan"})
    @GetMapping("/getRecruitInfoById")
    public Result getRecruitInfoById(@RequestParam int recruitId){
        RecruitDTO recruitDTO = artisanService.getRecruitInfoById(recruitId);
        return Result.create(recruitDTO);
    }

    /**
     * @param recruitId
     * @param title
     * @param num
     * @param start
     * @param end
     * @param requirements
     * @return
     */
    @PostMapping("/checkOK")
    public Result checkOK(@RequestParam int recruitId , @RequestParam String title , @RequestParam int num , @RequestParam String start,
                          @RequestParam String end , @RequestParam String requirements){
        artisanService.checkOK(recruitId,title,num,start,end,requirements);
        return Result.create("success");
    }

    /**
     * 审核不通过
     * @param recruitId
     * @return
     */
    @PostMapping("/checkNO")
    public Result checkNO(@RequestParam int recruitId){
        artisanService.checkNO(recruitId);
        return Result.create("success");
    }

    /**
     * 查看应聘我发布岗位的学生
     * @return
     */
    @GetMapping("/getApplyStudentByRecruitId")
    public Result getApplyStudentByRecruitId(HttpServletRequest request) throws BusinessException {
        String token = request.getHeader("TOKEN");
        String userId = JWT.decode(token).getClaim("userId").asString();
        List<ApplyDTO> list = artisanService.getApplyStudentByRecruitId(userId);
        return Result.create(list);
    }

    /**
     * 审核学生申请
     * @param applyId
     * @return
     */
    @GetMapping("/checkStudentApply")
    public Result checkStudentApply(@RequestParam int applyId,@RequestParam String email,@RequestParam String status){
        CheckDTO checkDTO = new CheckDTO();
        checkDTO.setApplyId(applyId);
        checkDTO.setEmail(email);
        checkDTO.setStatus(status);
        /**
         * 投放到消息队列
         */
        jmsMessagingTemplate.convertAndSend(topic2,checkDTO);
        return Result.create(null);
    }


    /**
     * 已审核过的学生列表
     * @return
     */
    @GetMapping("/alreadyCheckStudentList")
    public Result alreadyCheckStudentList(HttpServletRequest request){
        String token = request.getHeader("TOKEN");
        String userId = JWT.decode(token).getClaim("userId").asString();
        List<ApplyDTO> list = artisanService.alreadyCheckStudentList(userId);
        return Result.create(list);
    }

    /**
     * 发布面试结果
     * @param title
     * @param content
     * @return
     */
    @PostMapping("/releaseInterviewResult")
    public Result releaseInterviewResult(HttpServletRequest request , @RequestParam String title,@RequestParam String content){
        String token = request.getHeader("TOKEN");
        String userId = JWT.decode(token).getClaim("userId").asString();
        InterviewResultDTO interviewResultDTO = new InterviewResultDTO();
        interviewResultDTO.setContent(content);
        interviewResultDTO.setTitle(title);
        interviewResultDTO.setReleasePeople(userId);
        jmsMessagingTemplate.convertAndSend(topic3,interviewResultDTO);
        return Result.create(null);
    }
}
